#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : Moty
# @Time     : 2024/10/18 16:52
# @File     : kc16_1B.py
# @Project  : CSPT
def parse_1B_Error_Message(body):
    # 解析0x1B 错误报文
    data = {
        "交易流水号": f"{body[0:32]}({body[0:32]})",
        "桩编号": f"{body[32:46]}({body[32:46]})",
        "枪号": f"{int(body[46:48], 16)}枪({body[46:48]})",
        "接收 SPN2560=0x00 的充电机辨识报文超时": f"{error_message_type(bin(int(body[48:50],16))[2:].zfill(8)[0:2])}({bin(int(body[48:50],16))[2:].zfill(8)})",
        "接收 SPN2560=0xAA 的充电机辨识报文超时": f"{error_message_type(bin(int(body[48:50],16))[2:].zfill(8)[2:4])}({bin(int(body[48:50],16))[2:].zfill(8)})",
        "预留位": f"预留位(0000)",
        "接收充电机的时间同步和充电机最大输出能力报文超时": f"{error_message_type(bin(int(body[50:52],16))[2:].zfill(8)[0:2])}({bin(int(body[50:52],16))[2:].zfill(8)})",
        "接收充电机完成充电准备报文超时": f"{error_message_type(bin(int(body[50:52],16))[2:].zfill(8)[2:4])}({bin(int(body[50:52],16))[2:].zfill(8)})",
        "预留位2": f"预留位(0000)",
        "接收充电机充电状态报文超时": f"{error_message_type(bin(int(body[52:54],16))[2:].zfill(8)[0:2])}({bin(int(body[52:54],16))[2:].zfill(8)})",
        "接收充电机中止充电报文超时": f"{error_message_type(bin(int(body[52:54],16))[2:].zfill(8)[2:4])}({bin(int(body[52:54],16))[2:].zfill(8)})",
        "预留位3": f"预留位(0000)",
        "接收充电机充电统计报文超时": f"{error_message_type(bin(int(body[54:56],16))[2:].zfill(8)[0:2])}({bin(int(body[54:56],16))[2:].zfill(8)})",
        "BMS 其他": f"{bin(int(body[54:56],16))[2:].zfill(8)[2:8]}({bin(int(body[54:56],16))[2:].zfill(8)})",
        "接收 BMS 和车辆的辨识报文超时": f"{error_message_type(bin(int(body[56:58],16))[2:].zfill(8)[0:2])}({bin(int(body[56:58],16))[2:].zfill(8)})",
        "预留位4": f"预留位(000000)",
        "接收电池充电参数报文超时": f"{error_message_type(bin(int(body[58:60],16))[2:].zfill(8)[0:2])}({bin(int(body[58:60],16))[2:].zfill(8)})",
        "接收 BMS 完成充电准备报文超时": f"{error_message_type(bin(int(body[58:60],16))[2:].zfill(8)[2:4])}({bin(int(body[58:60],16))[2:].zfill(8)})",
        "预留位5": f"预留位(0000)",
        "接收电池充电总状态报文超时": f"{error_message_type(bin(int(body[60:62],16))[2:].zfill(8)[0:2])}({bin(int(body[60:62],16))[2:].zfill(8)})",
        "接收电池充电要求报文超时": f"{error_message_type(bin(int(body[60:62],16))[2:].zfill(8)[2:4])}({bin(int(body[60:62],16))[2:].zfill(8)})",
        "接收 BMS 中止充电报文超时": f"{error_message_type(bin(int(body[60:62],16))[2:].zfill(8)[4:6])}({bin(int(body[60:62],16))[2:].zfill(8)})",
        "预留位6": f"预留位(00)",
        "接收 BMS 充电统计报文超时": f"{error_message_type(bin(int(body[62:64],16))[2:].zfill(8)[0:2])}({bin(int(body[62:64],16))[2:].zfill(8)})",
        "充电机其他": f"{bin(int(body[62:64],16))[2:].zfill(8)[2:8]}({bin(int(body[62:64],16))[2:].zfill(8)})",
    }
    formatted_data = ""
    for key, value in data.items():
        formatted_data += f"{key}: {value}\n"
    return formatted_data


def error_message_type(result):
    return "正常" if result == "00" else "超时" if result == "01" else "不可信" if result == "10" else "未知"


__all__ = ['parse_1B_Error_Message']